<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">基本配置</div></el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><div class="div2">菜单按钮管理</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 teamManage -->
    <!-- 菜单栏 -->
    <div class="menuBtn">
      <el-tree
        class="el-menu-vertical-demo"
        :data="tableData"
        :props="defaultProps"
        :default-expand-all="true"
        :highlight-current="true"
        @node-click="handleNodeClick"
      ></el-tree>
    </div>
    <!-- 菜单栏 -->

    <!-- 管理团队 -->
    <div class="manage">
      <div class="btn">
        <span style="display: inline-block;font-size:15px;margin-right:20px;"
          >当前菜单名称：{{ ruleForm.fatherName }}</span
        >
        <el-button
          style="padding:8px 12px;"
          type="warning"
          @click="
            dialogFormVisible = true;
            openPop();
          "
          v-if="parArray.indexOf('addbtn') != -1"
          >新添按钮</el-button
        >
      </div>
      <div class="table">
        <el-form :inline="true">
          <el-form-item label="菜单名字" prop="name">
            <el-input v-model="searchName"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="btnlist()"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
        <vxe-table
          align="center"
          :data="list"
          highlight-hover-row
          height="600"
          style="width:100%;"
        >
          <vxe-table-column field="code" title="按钮编号"></vxe-table-column>
          <vxe-table-column field="name" title="按钮名称"></vxe-table-column>
          <vxe-table-column
            field="remark"
            title="备注"
            :show-overflow-tooltip="true"
          ></vxe-table-column>
          <vxe-table-column field="statusName" title="状态"></vxe-table-column>
          <vxe-table-column
            field="createdTime"
            title="创建时间"
            width="150"
          ></vxe-table-column>
          <vxe-table-column title="操作" width="200">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="编辑"
                placement="top-start"
              >
                <p
                  class="operation"
                  @click="fixBtn(scope.row)"
                  v-if="parArray.indexOf('menuFix') != -1"
                >
                  修改
                </p>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="top-start"
              >
                <p
                  class="operation"
                  @click="delectData(scope.$index, scope.row)"
                  style="color:#f40713;"
                  v-if="parArray.indexOf('menudelect') != -1"
                >
                  删除
                </p>
              </el-tooltip>
            </template>
          </vxe-table-column>
        </vxe-table>
      </div>
    </div>
    <!-- 管理客户等级 -->
    <!-- 对话弹框 -->
    <el-dialog
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="添加/修改按钮"
    >
      <span style="display: inline-block;font-size:15px;margin-right:20px;"
        >当前菜单名称：{{ ruleForm.fatherName }}</span
      >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="elForm"
      >
        <el-form-item label="按钮名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="按钮编码" prop="code">
          <el-input v-model="ruleForm.code"></el-input>
        </el-form-item>
        <el-form-item label="按钮备注" prop="desc">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="perm">
          <el-input v-model="ruleForm.perm"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="
            dialogFormVisible = false;
            submitForm('ruleForm');
          "
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 对话弹框 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchName: "",
      ruleForm: {
        name: "",
        menuType: "2",
        desc: "",
        fatherName: "",
        perm: "",
        winType:"",
      },
      rules: {
        name: [{ required: true, message: "请输入按钮名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入按钮编码", trigger: "blur" }],
        desc: [{ required: true, message: "请填写按钮备注", trigger: "blur" }],
      },
      tableData: [],
      list: [],
      dialogFormVisible: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      parentCode: "",
      menuId: "",
      btnId: "",
      limits: "",
      parArray: [],
    };
  },
  created() {
    this.listAxios();
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "menuBtnCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断
  },
  activated() {
    //  this.listAxios();
  },
  methods: {
    //** 查询菜单列表 **//
    listAxios() {
      this.axios
        .post("/sys/v1/menu/button/tree", {
          request: {
            name: "",
          },
          version: "",
        })
        .then((res) => {
          // console.log('查询菜单列表');
          // console.log(res);
          if (res.code == 0) {
            this.tableData = res.data;
            this.ruleForm.fatherName = res.data[0].name;
            this.ruleForm.winType=res.data[0].winType;
            this.btnlist(res.data[0].code);
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 按钮列表
    btnlist(code) {
      this.axios
        .post(
          "/sys/v1/menu/button/list",
          {
            request: {
              type: 2,
              parentCode: code,
              name: this.searchName,
            },
            version: "",
          },
          {
            headers: {
              isLoading: false,
            },
          }
        )
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            this.list = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 新添加按钮
    openPop() {
      this.ruleForm = {
        name: "",
        menuType: "2",
        desc: "",
        fatherName: this.ruleForm.fatherName,
        perm: "",
        winType:this.ruleForm.winType,
      };
      this.btnId = "";
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.btnId == "" || this.btnId == undefined) {
            this.axios
              .post("/sys/v1/menu/button/add", {
                request: {
                  userId: sessionStorage.getItem("user_id"),
                  name: this.ruleForm.name,
                  code: this.ruleForm.code,
                  parentCode: this.parentCode,
                  type: "2",
                  remark: this.ruleForm.desc,
                  sort: 0,
                  perms: this.ruleForm.perm,
                  winType:this.ruleForm.winType,
                },
              })
              .then((res) => {
                // console.log(res);
                if (res.code == 0) {
                  this.btnlist(this.parentCode);
                  this.$message.success("添加成功");
                  this.menuId == "";
                  this.ruleForm = {
                    name: "",
                    menuType: "2",
                    desc: "",
                    code: "",
                    fatherName: this.ruleForm.fatherName,
                    perm: "",
                    winType:this.ruleForm.winType,
                  };
                } else {
                  this.$message.error(res.message);
                }
              });
          } else {
            //修改
            this.axios
              .post("/sys/v1/menu/button/update", {
                request: {
                  userId: sessionStorage.getItem("user_id"),
                  id: this.btnId,
                  name: this.ruleForm.name,
                  code: this.ruleForm.code,
                  type: "2",
                  remark: this.ruleForm.desc,
                  sort: 0,
                  perms: this.ruleForm.perm,
                  winType:this.ruleForm.winType
                },
              })
              .then((res) => {
                // console.log(res);
                if (res.code == 0) {
                  this.searchName = "";
                  this.btnlist(this.parentCode);
                  this.$message.success("修改成功");
                  this.ruleForm = {
                    name: "",
                    menuType: "2",
                    desc: "",
                    code: "",
                    fatherName: this.ruleForm.fatherName,
                    perm: "",
                  };
                } else {
                  this.$message.error(res.message);
                }
              });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    //** 查询菜单列表 **//
    handleNodeClick(data) {
      // console.log(data);
      this.parentCode = data.code;
      this.menuId = data.id;
      this.ruleForm = {
        fatherName: data.name,
        name: data.name,
        menuType: "2",
        desc: data.typeName,
        code: data.code,
        perm: data.perms,
        winType:data.winType,
      };
      this.searchName = "";
      this.btnlist(data.code);
    },
    // 修改按钮
    fixBtn(row) {
      this.axios
        .get("/sys/v1/menu/button/detail?id=" + row.id, {
          headers: {
            isLoading: false,
          },
        })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            this.dialogFormVisible = true;
            this.btnId = row.id;
            this.ruleForm = {
              fatherName: res.data.name,
              name: res.data.name,
              menuType: "2",
              desc: res.data.remark,
              code: res.data.code,
              perm: res.data.perms,
              winType:res.data.winType,
            };
          }
        });
    },
    is_null(content) {
      if (content) {
        return false;
      } else {
        return true;
      }
    },
    delectData(index, rows) {
      // console.log("删除当前数据")
      // console.log(rows)
      this.$confirm("确定要删除该权限吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .post(
              "/sys/v1/menu/button/delete",
              {
                request: {
                  userId: sessionStorage.getItem("user_id"),
                  id: rows.id,
                },
              },
              {
                headers: {
                  isLoading: false,
                },
              }
            )
            .then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.$message.success("删除成功");
                this.btnlist(this.parentCode);
                rows.splice(index, 1);
                // this.parentCode="";
                this.if_father = false;
              }
            });
          // }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.div3 {
  margin-left: -8px;
  background: url(../../../assets/breadcrumb3.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
/deep/.el-dialog__header {
  background-color: #55aaff;
  padding: 10px 20px 10px;
}
/deep/.el-dialog__close {
  color: #fff;
}
/deep/.el-dialog__headerbtn {
  position: absolute;
  top: 15px;
  right: 20px;
  border: none;
  outline: 0;
  cursor: pointer;
  font-size: 16px;
}
/deep/.el-dialog__title {
  color: #fff;
  font-size: 16px;
}
.el-menu-vertical-demo {
  width: 200px;
  height: 750px;
  overflow-y: scroll;
  position: absolute;
  left: 0;
}
.el-menu-vertical-demo::-webkit-scrollbar {
  display: none;
}
.el-menu-vertical-demo {
  -ms-overflow-style: none;
}
.el-menu-vertical-demo {
  overflow: -moz-scrollbars-none;
}
.elForm {
  padding: 20px 10px;
}
.operation:hover {
  cursor: pointer;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
  position: absolute;
  top: 24px;
  left: 230px;
  width: 1200px;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  width: 100%;
  text-align: center;
  p {
    display: inline-block;
    vertical-align: middle;
    margin-left: 20px;
    img {
      width: 18px;
      height: 20px;
      margin-top: 8px;
    }
  }
}
.item {
  display: inline-block;
  margin-left: 10px;
  position: relative;
  img {
    width: 22px;
    height: 22px;
  }
}
.pover {
  display: inline-block;
  /deep/.el-button {
    padding: 8px;
  }
}
/deep/.el-form-item {
  // margin-bottom:0px;
}
/deep/.el-popover__title {
  font-size: 15px;
  color: #666;
}
.float {
  padding: 8px 10px !important;
}
.page {
  margin: 20px 0;
  text-align: right;
  width: 100%;
  span {
    font-size: 15px;
    color: #888;
    display: inline-block;
    margin-left: 10px;
    margin-top: -2px;
    vertical-align: middle;
  }
  .el-pagination {
    display: inline-block;
    vertical-align: middle;
  }
}
/deep/.el-table__row:hover {
  color: rgb(45, 125, 223);
}
/deep/.el-dialog {
  width: 55%;
  .el-input__inner {
    // width:300px;
  }
}
.add_btn {
  margin-bottom: 20px;
  /deep/.el-input__inner {
    width: 200px;
  }
  .el-form-item,
  .box {
    display: inline-block;
    vertical-align: middle;
    // width:300px;
  }
  img {
    display: inline-block;
    vertical-align: middle;
    margin-left: 20px;
    width: 22px;
    height: 22px;
  }
  p {
    display: inline-block;
    padding-top: 5px;
  }
}
.table {
  margin: 20px 0;
}
</style>
